// packageStore/components/realNameVerify/realNameVerify.js
const componentBehavior = require('../../../js/common/ComponentBehavior')
const {
  finance
} = require('../../../js/modules/store')
const {
  throttle,
  alert,
  confirm,
  isEmpty
} = require('../../../js/newUtil')

Component({
  behaviors: [componentBehavior],
  /**
   * 组件的属性列表
   */
  properties: {
    mainColor: { // 主题色
      type: String,
      value: '#e56b14',
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    showComponent: false, // 当前组件展示状态
    name: '', // 姓名
    idNum: '', // 身份证号
    phone: '', // 手机号
    smsCode: '', // 短信验证码
    timeCountDown: 0, // 获取验证码倒计时
    isUpload: false, // 是否已提交，展示提交成功的内容
    smsCodeInterval: null, // 短信验证码循环
    locked: false,
    refuseRemark: '', // 审核被拒绝时的备注信息
  },

  lifetimes: {
    detached() {
      if (this.data.smsCodeInterval) {
        clearInterval(this.data.smsCodeInterval)
      }
    },
  },

  /**
   * 组件的方法列表
   */
  methods: {
    showComponent () {
      let that = this
      finance.getRealNameVerifyStatus(function (res) {
        console.log(res)
        if (res.Success) {
          let content = res.Content || {}
          if (content.State == 1) {
            that.setData({
              showComponent: true,
              name: content.RealName || '',
              idNum: content.IDNumber || '',
              phone: content.Mobile || '',
              refuseRemark: content.Remark || ''
            })
          } else {
            that.setData({
              showComponent: true
            })
          }
        } else {
          that.setData({
            showComponent: true
          })
        }
      })
    },
    /**
     * 输入
     * @param {*} e 
     */
    input(e) {
      let name = e.currentTarget.dataset.name
      let value = e.detail.value
      this.setData({
        [name]: value
      })
    },
    /**
     * 获取手机短信验证码
     */
    getCode: throttle(function () {
      let {
        phone,
        timeCountDown
      } = this.data
      let that = this
      if (timeCountDown != 0) {
        return
      }
      let reg = /^1\d{10}$/
      if (!reg.test(phone)) {
        alert('请输入一个正确的手机号')
        return
      }
      finance.sendSmsCode(phone, function (res) {
        if (res.Success) {
          alert('验证码发送成功，请注意查收')
          clearInterval(that.smsCodeInterval)
          timeCountDown = 60
          that.smsCodeInterval = setInterval(() => {
            timeCountDown -= 1
            that.setData({
              timeCountDown
            })
            if (timeCountDown == 0) {
              clearInterval(that.smsCodeInterval)
            }
          }, 1000)
        } else {
          alert(res.Message)
        }
      })
    }, 1000),
    cancel: async function () {
      let confirmRes = await confirm({
        title: '是否返回？',
        content: '系统将不会记录您当前的输入内容'
      })
      if (!confirmRes.confirm) {
        return
      }
      this.setData({
        showComponent: false,
      })
      this.reset()
    },
    confirm: async function () {
      let that = this
      let {
        name,
        idNum,
        phone,
        smsCode
      } = this.data
      if (isEmpty(name)) {
        alert('请输入姓名')
        return
      }
      if (isEmpty(idNum)) {
        alert('请输入身份证号')
        return
      }
      if (idNum.length != 18) {
        alert('身份证号格式错误')
        return
      }
      if (isEmpty(phone)) {
        alert('请输入手机号')
        return
      }
      let reg = /^1\d{10}$/
      if (!reg.test(phone)) {
        alert('请输入一个正确的手机号')
        return
      }
      if (isEmpty(smsCode)) {
        alert('请输入短信验证码')
        return
      }
      if (this.data.locked) {
        return
      }
      this.setData({
        locked: true
      })
      finance.uploadRealNameVerify(phone, name, idNum, smsCode, function (res) {
        if (res.Success) {
          alert('实名认证资料已提交')
          that.setData({
            isUpload: true
          })
          that.triggerEvent('uploadVerifyComplete')
        } else {
          alert({
            content: res.Message
          })
        }
        that.setData({
          locked: false
        })
      })
    },
    completeCancel () {
      this.setData({
        showComponent: false,
      })
      this.reset()
      clearInterval(this.data.smsCodeInterval)
    },
    reset () {
      this.setData({
        name: '',
        phone: '',
        idNum: '',
        smsCode: '',
        isUpload: false,
        locked: false
      })
      wx.setNavigationBarTitle({
        title: '提现',
      })
      clearInterval(this.data.smsCodeInterval)
    },
  }
})